package com.example.grantsys.api;

import cn.hutool.crypto.SecureUtil;
import com.example.grantsys.domain.Terminal;
import com.example.grantsys.domain.User;
import com.example.grantsys.req.ClientReq;
import com.example.grantsys.req.UserReq;
import com.example.grantsys.restful.ResultVO;
import com.example.grantsys.service.TerminalService;
import com.example.grantsys.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "注册管理")
@RestController
public class RegisterApi {

    @Autowired
    private TerminalService terminalService;
    @Autowired
    private UserService userService;


    @PostMapping("/registerTerminal")
    @ApiOperation("终端注册")
    public ResultVO<Terminal> addNewTerminal(@RequestBody @Validated ClientReq req) {
        Terminal terminal = new Terminal();
        terminal.setName(req.getClientId());
        terminal.setSecret(SecureUtil.md5(req.getClientSecret()));
        terminalService.save(terminal);
        return ResultVO.ok(terminal);
    }


    @PostMapping("/registerUser")
    @ApiOperation("用户注册")
    public ResultVO addNewUser(@RequestBody @Validated UserReq req) {
        User user = new User();
        user.setAccount(req.getAccount());
        user.setPassword(SecureUtil.md5(req.getPassword()));
        user.setAge(req.getAge());
        user.setEmail(req.getEmail());
        user.setName(req.getName());
        userService.save(user);
        return ResultVO.ok();
    }


}
